﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace ASPItalia.ModelVirtualCasting.Silverlight.Panels
{
    public class Panel3D : Panel
    {
  
        public static readonly DependencyProperty FocusedItemIndexProperty
          = DependencyProperty.Register("FocusedItemIndex", typeof(int), typeof(Panel3D), new PropertyMetadata(Panel3D.PropertyChanged));

        public Panel3D()
        {
            FocusedItemIndex = 0;
        }

        private static void PropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            ((Panel3D)sender).InvalidateArrange();
        }

        protected override Size MeasureOverride(Size availableSize)
        {
            for (int i = 0; i < Children.Count; i++)
			{
               ((FrameworkElement)Children[i]).Measure(availableSize);
			}
            double _width = availableSize.Width;
            double _height = availableSize.Height;

           return new Size(_width, _height);
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
        
            for (int i = 0; i < Children.Count; i++)
            {
                double relIndex = i - FocusedItemIndex;
                double absRelIndex = Math.Abs(relIndex);
              
                FrameworkElement item = (FrameworkElement)Children[i];
                Canvas.SetZIndex(item, i);
                Point center = new Point(-item.DesiredSize.Width, (finalSize.Height - item.DesiredSize.Height) / 2);               
                item.Arrange(new Rect(center.X, center.Y, item.DesiredSize.Width, item.DesiredSize.Height));               
                Storyboard storyboard = new Storyboard();
                item.Projection =  new PlaneProjection{ RotationY = 40};         
                DoubleAnimation Xanim = new DoubleAnimation();
                DoubleAnimation Zanim = new DoubleAnimation();
               
                Storyboard.SetTarget(Xanim, item.Projection);
                Storyboard.SetTargetProperty(Xanim, new PropertyPath(PlaneProjection.GlobalOffsetXProperty));
                Storyboard.SetTarget(Zanim, item.Projection);
                Storyboard.SetTargetProperty(Zanim, new PropertyPath(PlaneProjection.GlobalOffsetZProperty));

                Xanim.To = Math.Sqrt( i + 1) * 160 ;
                Zanim.To = -40 *  ( i + 1);  
                Xanim.Duration = TimeSpan.FromSeconds(0.3);
                Zanim.Duration = TimeSpan.FromSeconds(0.3);
                Zanim.BeginTime = TimeSpan.FromSeconds(0.29);
                Xanim.BeginTime = TimeSpan.FromSeconds(0.12);
                //timer per aggiustare lo z-index
                DispatcherTimer zindexTimer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(0.35 *i) };
                zindexTimer.Tick += (sender, e) => { Canvas.SetZIndex(item, 0); zindexTimer.Stop(); };
                storyboard.Children.Add(Xanim);
                storyboard.Children.Add(Zanim);
                storyboard.BeginTime = TimeSpan.FromSeconds(0.3 * (i));
                zindexTimer.Start();
                storyboard.Begin();   
            }
  
            return finalSize;
        }


        public int FocusedItemIndex
        {
            get { return (int)this.GetValue(Panel3D.FocusedItemIndexProperty); }
            set { this.SetValue(Panel3D.FocusedItemIndexProperty, value); }
        }
    }
}
